package com.ruoyi.ldzlsc.service.Impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.ldzlsc.mapper.ScSalesOrderMapper;
import com.ruoyi.ldzlsc.mapper.ScSalesOrderDetailMapper;
import com.ruoyi.ldzlsc.domain.ScSalesOrder;
import com.ruoyi.ldzlsc.domain.ScSalesOrderDetail;
import com.ruoyi.ldzlsc.service.ScSalesOrderService;

/**
 * 销售订单Service业务层处理
 *
 * @author ldzl
 * @date 2025-05-27
 */
@Service
public class ScSalesOrderServiceImpl implements ScSalesOrderService {
    @Autowired
    private ScSalesOrderMapper scSalesOrderMapper;
    
    @Autowired
    private ScSalesOrderDetailMapper scSalesOrderDetailMapper;

    /**
     * 查询销售订单列表
     *
     * @param scSalesOrder 销售订单
     * @return 销售订单
     */
    @Override
    public List<ScSalesOrder> selectScSalesOrderList(ScSalesOrder scSalesOrder) {
        return scSalesOrderMapper.selectScSalesOrderList(scSalesOrder);
    }
    
    /**
     * 查询销售订单明细列表
     *
     * @param salesOrderId 销售订单ID
     * @return 销售订单明细集合
     */
    @Override
    public List<ScSalesOrderDetail> selectScSalesOrderDetailList(Long salesOrderId) {
        return scSalesOrderDetailMapper.selectScSalesOrderDetailBySalesOrderId(salesOrderId);
    }
    
    /**
     * 查询销售订单信息
     *
     * @param id 销售订单ID
     * @return 销售订单信息
     */
    @Override
    public ScSalesOrder selectScSalesOrderById(Long id) {
        return scSalesOrderMapper.selectScSalesOrderById(id);
    }

    /**
     * 新增销售订单
     *
     * @param scSalesOrder 销售订单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertScSalesOrder(ScSalesOrder scSalesOrder) {
        int rows = scSalesOrderMapper.insertScSalesOrder(scSalesOrder);
        List<ScSalesOrderDetail> details = scSalesOrder.getDetails();
        if (details != null && !details.isEmpty()) {
            Long salesOrderId = scSalesOrder.getSalesOrderId();
            if (salesOrderId == null) {
                throw new RuntimeException("插入销售订单失败，无法获取到主键ID。");
            }
            for (ScSalesOrderDetail detail : details) {
                detail.setSalesOrderId(salesOrderId);
                scSalesOrderDetailMapper.insertScSalesOrderDetail(detail);
            }
        }
        return rows;
    }

    /**
     * 修改销售订单
     *
     * @param scSalesOrder 销售订单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateScSalesOrder(ScSalesOrder scSalesOrder) {
        // 1. 更新主订单
        int rows = scSalesOrderMapper.updateScSalesOrder(scSalesOrder);

        // 2. 先获取新的列表
        List<ScSalesOrderDetail> newDetails = scSalesOrder.getDetails();
        Long salesOrderId = scSalesOrder.getSalesOrderId();

        // 3. 处理明细的增、改、删
        if (newDetails != null) {
            // 原始的列表
            List<Long> oldDetailIds = scSalesOrderDetailMapper.selectScSalesOrderDetailBySalesOrderId(salesOrderId)
                    .stream()
                    .map(ScSalesOrderDetail::getSalesOrderDetailId)
                    .collect(Collectors.toList());
            List<Long> newDetailIds = new ArrayList<>();
            for (ScSalesOrderDetail detail : newDetails) {
                detail.setSalesOrderId(salesOrderId);
                if (detail.getSalesOrderDetailId() != null) {
                    // 这是已存在的明细，进行更新
                    scSalesOrderDetailMapper.updateScSalesOrderDetail(detail);
                    newDetailIds.add(detail.getSalesOrderDetailId());
                } else {
                    // 这是新增的明细，进行插入
                    scSalesOrderDetailMapper.insertScSalesOrderDetail(detail);
                }
            }

            // 找出需要删除的明细ID
            oldDetailIds.removeAll(newDetailIds);
            if (!oldDetailIds.isEmpty()) {
                scSalesOrderDetailMapper.deleteScSalesOrderDetailByIds(oldDetailIds.toArray(new Long[0]));
            }
        } else {
            // 如果新的明细列表为空，则删除所有旧的明细
            scSalesOrderDetailMapper.deleteScSalesOrderDetailBySalesOrderId(salesOrderId);
        }

        return rows;
    }


    /**
     * 批量删除销售订单信息
     *
     * @param ids 需要删除的销售订单ID数组
     * @return 结果
     */
    @Override
    public int deleteScSalesOrderByIds(Long[] ids) {
        for (Long id : ids) {
            // 删除销售订单明细
            scSalesOrderDetailMapper.deleteScSalesOrderDetailBySalesOrderId(id);
        }
        // 删除销售订单
        return scSalesOrderMapper.deleteScSalesOrderByIds(ids);
    }
}
